<template>
  <div class="app-container">

    <div v-if="crud.props.searchToggle">
      <el-input v-model="query.name" clearable size="small" placeholder="输入名称搜索" style="width: 200px;"
                class="filter-item" @keyup.enter.native="crud.toQuery"/>
      <rrOperation/>
    </div>

    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="医院" prop="hpId">
            <template>
              <el-select v-model="form.hpId" placeholder="请选择" @change="changeEvent">
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.mnName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </template>
          </el-form-item>

          <el-form-item label="科室" prop="officeId">
            <template>
              <el-select v-model="form.officeId" multiple placeholder="请选择" @change="changeValue">
                <el-option
                  collapse-tags
                  v-for="item in offices"
                  :key="item.id"
                  :label="item.mnName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </template>
          </el-form-item>

          <el-form-item label="知名专家" prop="expertIds">
            <template>
              <el-select v-model="form.expertIds" multiple placeholder="请选择">
                <el-option
                  collapse-tags
                  v-for="item in doctors"
                  :key="item.id"
                  :label="item.mnName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </template>
          </el-form-item>
          <el-form-item label="分类" prop="type">
            <el-select v-model="form.type" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.illness_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="名称(蒙)" prop="mnName">
            <el-input v-model="form.mnName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="名称(汉)" prop="zhName">
            <el-input v-model="form.zhName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="疾病定义(蒙)" >
            <el-input v-model="form.mnDefinition" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="疾病定义(汉)" >
            <el-input v-model="form.zhDefinition" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="诊断(蒙)" >
            <el-input v-model="form.mnDiagnosis" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="诊断(汉)" >
            <el-input v-model="form.zhDiagnosis" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="病因与病机(蒙)" >
            <el-input v-model="form.mnPathogenesis" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="病因与疾病(汉)" >
            <el-input v-model="form.zhPathogenesis" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="临床表现(蒙)" >
            <el-input v-model="form.mnClinical" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="临床表现(汉)" >
            <el-input v-model="form.zhClinical" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="治疗重点(蒙)">
            <el-input v-model="form.mnTreatment" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="治疗重点(汉)" >
            <el-input v-model="form.zhTreatment" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="治疗原则(蒙)" >
            <el-input v-model="form.mnPrinciple" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="治疗原则(汉)">
            <el-input v-model="form.zhPrinciple" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="治疗方法(蒙)" >
            <el-input v-model="form.mnTreatmentMethod" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="治疗方法(汉)" >
            <el-input v-model="form.zhTreatmentMethod" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="护理与预防(蒙)" >
            <el-input v-model="form.mnPrevention" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="护理与预防(汉)" >
            <el-input v-model="form.zhPrevention" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="传染性" prop="transmissibility">
            <el-select v-model="form.transmissibility" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.transmissibility"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="严重性" prop="seriousness">
            <el-select v-model="form.seriousness" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.seriousness"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="治愈率" prop="recoveryRate">
            <el-select v-model="form.recoveryRate" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.recovery_rate"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="科学研究(蒙)" >
            <el-input v-model="form.mnScientificResearch" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="科学研究(汉)" >
            <el-input v-model="form.zhScientificResearch" type="textarea" style="width: 370px;"/>
          </el-form-item>

          <el-form-item label="推荐医院" prop="recommendHp">
            <template>
              <el-select v-model="form.recommendHp" placeholder="请选择" multiple>
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.mnName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </template>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="状态">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="id"/>
        <el-table-column prop="hpName" label="医院"/>
        <el-table-column prop="type" label="分类">
          <template slot-scope="scope">
            {{ dict.label.illness_type[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="mnName" label="名称(蒙)"/>
        <el-table-column prop="zhName" label="名称(汉)"/>
        <el-table-column prop="zhDefinition" label="疾病定义(汉)"/>
        <el-table-column prop="zhDiagnosis" label="蒙语诊断(汉)"/>
        <el-table-column prop="zhPathogenesis" label="病因与病机(汉)"/>
        <el-table-column prop="zhClinical" label="临床表现(汉)"/>
        <el-table-column prop="zhTreatmentMethod" label="治疗方法(汉)"/>
        <el-table-column prop="transmissibility" label="传染性">
          <template slot-scope="scope">
            {{ dict.label.transmissibility[scope.row.transmissibility] }}
          </template>
        </el-table-column>
        <el-table-column prop="seriousness" label="严重性">
          <template slot-scope="scope">
            {{ dict.label.seriousness[scope.row.seriousness] }}
          </template>
        </el-table-column>
        <el-table-column prop="recoveryRate" label="治愈率">
          <template slot-scope="scope">
            {{ dict.label.recovery_rate[scope.row.recoveryRate] }}
          </template>
        </el-table-column>
        <el-table-column prop="createBy" label="创建者"/>
        <el-table-column prop="updateBy" label="修改者"/>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeState(scope.row,scope.row.status)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','illness:edit','illness:del'])" label="操作" width="150px"
                         align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudIllness from '@/api/medicine/illness'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {getNameAndId} from "@/api/medicine/hospital";
import {getOfficeByHpId} from "../../../api/medicine/administrativeOffice";
import {getByName, getExpert} from "../../../api/medicine/illness";

const defaultForm = {
  id: null,
  hpId: null,
  officeId: null,
  expertIds: null,
  type: null,
  mnName: null,
  mnNameCode: null,
  zhName: null,
  mnDefinition: null,
  zhDefinition: null,
  mnDiagnosis: null,
  mnDiagnosisCode: null,
  zhDiagnosis: null,
  mnPathogenesis: null,
  mnPathogenesisCode: null,
  zhPathogenesis: null,
  mnClinical: null,
  mnClinicalCode: null,
  zhClinical: null,
  mnTreatment: null,
  mnTreatmentCode: null,
  zhTreatment: null,
  mnPrinciple: null,
  mnPrincipleCode: null,
  zhPrinciple: null,
  mnTreatmentMethod: null,
  mnTreatmentMethodCode: null,
  zhTreatmentMethod: null,
  mnPrevention: null,
  mnPreventionCode: null,
  zhPrevention: null,
  transmissibility: null,
  seriousness: null,
  recoveryRate: null,
  mnScientificResearch: null,
  zhScientificResearch: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  recommendHp: null,
  status: null,
  remark: null
}
export default {
  name: 'Illness',
  components: {pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['illness_type', 'transmissibility', 'seriousness', 'recovery_rate'],
  cruds() {
    return CRUD({title: '疾病管理', url: 'api/illness', idField: 'id', sort: 'id,desc', crudMethod: {...crudIllness}})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'illness:add'],
        edit: ['admin', 'illness:edit'],
        del: ['admin', 'illness:del']
      },
      rules: {
        hpId: [
          {required: true, message: '请选择医院', trigger: 'change'}
        ],
        officeId: [
          {required: true, message: '请先选择医院在选择科室', trigger: 'change'}
        ],
        expertIds: [
          {required: true, message: '请先选择科室在选择专家', trigger: 'change'}
        ],
        type: [
          {required: true, message: '请选择分类', trigger: 'change'}
        ],
        mnName: [
          {required: true, message: '请输入名称(蒙)', trigger: 'change'}
        ],
        zhName: [
          {required: true, message: '请输入名称(汉)', trigger: 'change'}
        ],
        mnDefinition: [
          {required: true, message: '请输入疾病定义(蒙)', trigger: 'change'}
        ],
        zhDefinition: [
          {required: true, message: '请输入疾病定义(汉)', trigger: 'change'}
        ],
        mnDiagnosis: [
          {required: true, message: '请输入蒙语诊断(蒙)', trigger: 'change'}
        ],
        zhDiagnosis: [
          {required: true, message: '请输入蒙语诊断(汉)', trigger: 'change'}
        ],
        mnPathogenesis: [
          {required: true, message: '请输入病因与病机(蒙)', trigger: 'change'}
        ],
        zhPathogenesis: [
          {required: true, message: '请输入病因与病机(汉)', trigger: 'change'}
        ],
        mnClinical: [
          {required: true, message: '请输入临床表现(蒙)', trigger: 'change'}
        ],
        zhClinical: [
          {required: true, message: '请输入临床表现(汉)', trigger: 'change'}
        ],
        mnTreatment: [
          {required: true, message: '请输入治疗重点(蒙)', trigger: 'change'}
        ],
        zhTreatment: [
          {required: true, message: '请输入治疗重点(汉)', trigger: 'change'}
        ],
        mnPrinciple: [
          {required: true, message: '请输入治疗原则(蒙)', trigger: 'change'}
        ],
        zhPrinciple: [
          {required: true, message: '请输入治疗原则(汉)', trigger: 'change'}
        ],
        mnTreatmentMethod: [
          {required: true, message: '请输入治疗方法(蒙)', trigger: 'change'}
        ],
        zhTreatmentMethod: [
          {required: true, message: '请输入治疗原则(汉)', trigger: 'change'}
        ],
        mnPrevention: [
          {required: true, message: '请输入护理与预防(蒙)', trigger: 'change'}
        ],
        transmissibility: [
          {required: true, message: '请选择传染性', trigger: 'change'}
        ],
        seriousness: [
          {required: true, message: '请选择严重性', trigger: 'change'}
        ],
        recoveryRate: [
          {required: true, message: '请选择治愈率', trigger: 'change'}
        ],
        mnScientificResearch: [
          {required: true, message: '请输入科学研究(蒙)', trigger: 'change'}
        ],
        zhScientificResearch: [
          {required: true, message: '请输入科学研究(汉)', trigger: 'change'}
        ],
        recommendHp: [
          {required: true, message: '请选择推荐医院', trigger: 'change'}
        ]
      },
      options: [],
      value: '',
      offices: [],
      doctors: [],
      input: ''
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    changeEvent(hpId) {
      getOfficeByHpId(hpId).then(res => {
        this.form.officeId = []
        this.offices = res;
      })
    },
    changeState(data, val) {
      this.$confirm('此操作将修改状态值为 ' + val + ' 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudIllness.edit(data).then(res => {
          if (val === 1) {
            data.status = 0;
          } else {
            data.status = 1;
          }
          this.$message({
            type: 'waring',
            message: '修改成功'
          });
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });
    },
    changeValue() {
      if (this.form.officeId.length > 0) {
        getExpert(this.form.officeId).then(res => {
          this.form.expertIds = [];
          this.doctors = res;
        })
      }
    },
    search() {
      getByName(encodeURIComponent(this.input)).then(res => {
        this.crud.data = res;
      })
    }
  },
  mounted() {
    getNameAndId().then(res => {
      this.options = res
    })

  }
}
</script>

<style scoped>

</style>
